package com.raos.example;

import com.raos.example.service.Assistant;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;

/**
 * 聊天 webAPI
 *
 * @author raos
 * @date 2025-04-02
 */
@Slf4j
@Mapping("/api/langchain")
@Controller
public class ChatAPI {

    @Inject(value = "qwenChatModel")
    ChatLanguageModel chatLanguageModel;

    @Inject(value = "assistant")
    private Assistant assistant;

    /**
     * langchain4j 底层API对话能力
     */
    @Mapping("/low/chat")
    public String lowChat(@Param(value = "message") String message) {
        log.info("message=[{}]", message);
        // Low Level API（底层API能力）
        String chat = chatLanguageModel.chat(UserMessage.from(message)).aiMessage().text();
        log.info("chat=[{}]", chat);
        return chat;
    }

    /**
     * langchain4j 高层API对话能力
     */
    @Mapping("/high/chat")
    public String highChat(@Param(value = "message") String message) {
        log.info("message=[{}]", message);
        String chat = assistant.chat(message);
        log.info("chat=[{}]", chat);
        return chat;
    }

}